package org.loong.crypto.service.software.provider.key;

import javax.crypto.SecretKey;

import org.loong.crypto.common.exception.CryptoException;
import org.loong.crypto.common.exception.InvalidKeyLengthException;
import org.loong.crypto.core.algorithm.KeyAlgorithm;
import org.loong.crypto.service.core.provider.KeyGenerator;
import org.loong.crypto.service.core.provider.impl.BaseKeyGeneratorProvider;

import cn.hutool.crypto.KeyUtil;

/**
 * Generic Key generator.
 */
public class GenericKeyGenerator extends BaseKeyGeneratorProvider implements KeyGenerator  {

    @Override
    public SecretKey generateKey(KeyAlgorithm algorithm, int keySize) throws CryptoException {
        if (algorithm.getKeyLength() > 0 && keySize < algorithm.getKeyLength()) {
            throw new InvalidKeyLengthException(algorithm.getKeyLength());
        }

        return KeyUtil.generateKey(algorithm.getName(), keySize);
    }
}
